package com.szy.douyou_gateway.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szy.douyou_gateway.config.ResultDTO;
import com.szy.douyou_gateway.config.util.JwtUtils;
import com.szy.douyou_gateway.dao.UserReq;
import com.szy.douyou_gateway.dao.Users;
import com.szy.douyou_gateway.dao.mapper.UsersMapper;
import com.szy.douyou_gateway.service.UsersService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author sunziyan
 * @date 2022/2/22
 */
@Service
public class UsersServiceImpl implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private JwtUtils jwtUtils;

    @Override
    public Users getUserById(Long userId) {
        Users user = usersMapper.selectById(userId);
        return user;
    }

    @Override
    public ResultDTO<Users> getUserByUserName(String userName, String password) {
        Users user = usersMapper.selectOne(new QueryWrapper<Users>().eq("user_name", userName));
        if (user == null) {
            return ResultDTO.fail("用户不存在");
        }
        if (!user.getUserPassword().equals(SecureUtil.md5(password))) {
            return ResultDTO.fail("密码不正确");
        }

        return ResultDTO.success(user);
    }

    @Override
    public ResultDTO addUser(Users user) {

        Users userExist = usersMapper.selectOne(new QueryWrapper<Users>().eq("user_name", user.getUserName()));
        if(userExist != null){
            return ResultDTO.fail("该用户名已被注册");
        }

        user.setUserPassword(SecureUtil.md5(user.getUserPassword()));
        int result = usersMapper.insert(user);
        if (result == 0) {
            return ResultDTO.fail("注册失败");
        }
        return ResultDTO.success(result);
    }

    @Override
    public ResultDTO updateOneUser(Users user){
        int result = usersMapper.updateById(user);
        if(result == 0){
            return ResultDTO.fail("更新用户信息失败");
        }
        return ResultDTO.success(result);
    }

    @Override
    public ResultDTO<IPage<Users>> queryUser(UserReq userReq) {
        Page<Users> usersPage = new Page<>(userReq.getPageNo(), userReq.getPageSize());
        IPage<Users> userList = usersMapper.selectPage(usersPage,new QueryWrapper<Users>().like("user_name",userReq.getUserName()));

        return ResultDTO.success(userList);
    }

    @Override
    public ResultDTO deleteUser(Long userId) {
        int result = usersMapper.deleteById(userId);
        if(result == 0){
            return ResultDTO.fail("删除失败");
        }
        return ResultDTO.success(result);
    }

    @Override
    public ResultDTO changePassword(Long userId, String userPassword) {
        Users users = new Users();
        users.setUserId(userId);
        users.setUserPassword(SecureUtil.md5(userPassword));
        Integer result = usersMapper.updateById(users);
        return ResultDTO.success(result);
    }


}
